'use strict';

angular.module('App').controller('uploadListController',
    ['$scope', '$stateParams', 'BaseModule', 'BaseFunction', '$location', 'Authentication', 'FileUploader',
    function($scope, $stateParams, BaseModule, BaseFunction, $location, Authentication, FileUploader) {
		$scope.FileItems = [{DisplayName: "文件"}, {DisplayName: "显示图标"}, {DisplayName: "按下图标"}];

        var focusedFileItemIndex = -1;
        $scope.selectFile = function(fileItemIndex){
            focusedFileItemIndex = fileItemIndex;
            document.getElementById("singleFile").click();
        }
        $scope.upload = function(fileItemIndex){
            focusedFileItemIndex = fileItemIndex;
            $scope.FileItems[fileItemIndex].FileItem.upload();
        }
        $scope.cancel = function(fileItemIndex){
            focusedFileItemIndex = fileItemIndex;
            $scope.FileItems[fileItemIndex].FileItem.cancel();
        }
        $scope.remove = function(fileItemIndex){
            focusedFileItemIndex = fileItemIndex;
            $scope.FileItems[fileItemIndex].FileItem.remove();
            $scope.FileItems[fileItemIndex].isSelected = false;
            $scope.FileItems[fileItemIndex].FileItem = null;
        }

        var setUploadFiles = function(){
            var uploadFiles = [];
            for(var i=0; i<$scope.FileItems.length; i++){
                if($scope.FileItems[i].FileItem != null){
                    uploadFiles.push($scope.FileItems[i].FileItem);
                }
            };
            uploader.queue = uploadFiles;
        }

        var uploader = $scope.uploader = new FileUploader({url: '/api/uploadFile'});
         // FILTERS 
         uploader.filters.push({name: 'customFilter', 
            //1，客户端上传文件时触发，每上传一个触发一次，限制上传的文件个数
             fn: function(fileItem, options) { 
                 return this.queue.length < 10;
             } 
         });  
 
         // CALLBACKS 
         //客户端上传失败时触发
         uploader.onWhenAddingFileFailed = function(item /*{File|FileLikeObject}*/, filter, options) { 
         }; 
         //2，客户端上传完毕时触发
         uploader.onAfterAddingFile = function(fileItem) {
            //在此可添加其他上传参数
            fileItem.formData.push({"FileName": 4444444});
            fileItem.formData.push({"OldPath": 4444444});
            fileItem.formData["Index"] = focusedFileItemIndex;

            $scope.FileItems[focusedFileItemIndex].FileItem = fileItem;
            $scope.FileItems[focusedFileItemIndex].isSelected = true;
            setUploadFiles();
         }; 
         //3，客户端所有文件上传完毕时触发
         uploader.onAfterAddingAll = function(addedFileItems) { 
         }; 
         //（1）上传给服务器之前触发
         uploader.onBeforeUploadItem = function(item) { 
         }; 
         //（2）单个文件上传给服务器时，进度发生变化触发
         uploader.onProgressItem = function(fileItem, progress) { 
         };
         //（3）全部文件上传给服务器时，总进度发生变化触发
         uploader.onProgressAll = function(progress) { 
         }; 
         //(4)单个文件上传，成功时触发
         uploader.onSuccessItem = function(fileItem, response, status, headers) { 
            console.log(response.FileItem.file.path);
         }; 
         //(4)单个文件上传服务器，失败时触发
         uploader.onErrorItem = function(fileItem, response, status, headers) { 
         };
         //单个文件上传，取消时触发
         uploader.onCancelItem = function(fileItem, response, status, headers) { 
         }; 
         //(5)单个文件上传完成时触发，包括上传失败
         uploader.onCompleteItem = function(fileItem, response, status, headers) { 
         }; 
         //(6)所有文件上传完成时触发，包括上传失败
         uploader.onCompleteAll = function() { 
         };
    }
]);